﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure;

namespace RegCustomer
{

    public class Customer : AggregateRoot
    {
        private Guid _personId;
        private string _name;
        private bool _active;
        private bool _priority;
        private DateTime dateOfBirth;

        public Customer()
        { }

        public override Guid Id
        {
            get { return _personId; }
        }

        private void Apply(CustomerCreatedEvent evt)
        {
            _personId = evt.PersonId;
            _name = evt.Name;
            _active = true;
            _priority = true;
        }

        private void Apply(CustomerDeactivatedEvent evt)
        {
            _active = false;
        }

        private void Apply(DateOfBirthUpdatedEvent evt)
        {
            _active = false;
        }

        private void Apply(CustomerUpdatedEvent evt)
        {
            _name = evt.NewName;
        }

        private void Apply(CustomerMadePriorityEvent evt)
        {
            _priority = false;
        }

        public Customer(Guid personId, string name)
        {
            State nextState = GetNextStateBasedOn(typeof(CustomerCreatedEvent));
            ApplyChange(new CustomerCreatedEvent(personId, _name, nextState));
        }

        public void UpdateName(string newName)
        {
            State nextState = GetNextStateBasedOn(typeof(CustomerUpdatedEvent));
            ApplyChange(new CustomerUpdatedEvent(_personId, newName, nextState));
        }

        public void MarkCustomerAsInvalid(Guid guid, string reason)
        {
            State nextState = GetNextStateBasedOn(typeof(CustomerDeactivatedEvent));
            ApplyChange(new CustomerDeactivatedEvent(guid, reason, nextState));
        }

        public void MakeCustomerPriorityCommand(Guid guid)
        {
            State nextState = GetNextStateBasedOn(typeof(CustomerMadePriorityEvent));
            ApplyChange(new CustomerMadePriorityEvent(guid, nextState));
        }

        public void UpdateDateOfBirth(Guid guid, DateTime dateOfBirth)
        {
            State nextState = GetNextStateBasedOn(typeof(DateOfBirthUpdatedEvent));
            ApplyChange(new DateOfBirthUpdatedEvent(guid, dateOfBirth, nextState));

            //TimeSpan span = DateTime.Now.Subtract(dateOfBirth);
            //if ((int)(span.Days / 365.25) > 70)
            //{
            //    nextState = GetNextStateBasedOn(typeof(CustomerMadePriorityEvent));
            //    ApplyChange(new CustomerMadePriorityEvent(guid, nextState));
            //}


        }
    }


}
